#include "stm32f4xx_hal.h"
#include "stm32f4xx_ll_rcc.h"
#include "stdio.h"
#include "log.h"

#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"

#include "DevicesPower.h"
#include "DevicesQueue.h"
#include "DevicesKey.h"

#include "DriverVoltage.h"

#include "taskKey.h"


/* 按键处理任务句柄 */
TaskHandle_t g_TaskKeyHand = NULL;


void vTaskKey(void *pvParameters)
{
    uint32_t uiNotifiedValue = 0;
    KeyTypeDef typeKeyData = {0};
    VoltageInfoType *ptypeVoltageInfoHandle = ptypeVoltageInfoGet();

    while(1)
    {
        ulTaskNotifyTake(pdTRUE, portMAX_DELAY);

        /* Key 触发的键值 */
        while(enumQueuePopDatas(&g_TypeQueueKeyInput, &typeKeyData, sizeof(KeyTypeDef)) == queueNormal)
        {
            /* 短按，并已松开 */
            if(typeKeyData.state == (keyShort | keyCut))
            {
                if(typeKeyData.valueLoosen & KEY_USER)
                {

                }
            }
            /* 长按，还未松开 */
            else if(typeKeyData.state == keyLong)
            {
                if(typeKeyData.valuePress & KEY_USER)
                {
                    cVoltageSet(VOLTAGE_CHANNEL_VDD1,   ptypeVoltageInfoHandle->vdd1.set);
                    cVoltageSet(VOLTAGE_CHANNEL_VDD2H,  ptypeVoltageInfoHandle->vdd2H.set);
                    cVoltageSet(VOLTAGE_CHANNEL_VDD2L,  ptypeVoltageInfoHandle->vdd2L.set);
                    cVoltageSet(VOLTAGE_CHANNEL_VDDQ,   ptypeVoltageInfoHandle->vddQ.set);

                    /* 设置完，延时一会后，再打开电源使能IO（调试时使用） */
                    vTaskDelay(20 / portTICK_RATE_MS);

                    vPowerOpen(POWER_CHANNEL_ALL);
                }
            }
        }
    }
}
